package cn.com.inossem.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.com.inossem.activity.FriendList;
import cn.com.inossem.activity.FriendsDetailsActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.model.FriendInfoBean;
import cn.com.inossem.model.UserInfo;
import cn.com.inossem.utils.Utils;

public class UserAdapter extends BaseAdapter {
	private List<UserInfo> taskList;
	private LayoutInflater mLayoutInflater;
	private ViewHolder viewHolder;
	// private UserInfo task;
	private String[] stringArr;
	private ArrayList<String> webNameArr;
	FriendInfoBean user_click = null;
	Context context;
	private OnClickListener listener;

	// public static ArrayList<String> mContactsName = new ArrayList<String>();
	private static class ViewHolder {
		ImageView iv_type;
		TextView tv_name;
		TextView tv_detail;
		TextView tv_btn;
		TextView textView3;
		TextView firstCharHintTextView;
	}

	FriendList friend_list = null;

	public UserAdapter(Context context, List<UserInfo> taskList,
	                   ArrayList<String> webNameArr, OnClickListener listener) {
		this.mLayoutInflater = LayoutInflater.from(context);
		this.context = context;
		this.taskList = taskList;
		this.friend_list = friend_list;
		this.webNameArr = webNameArr;
		this.listener = listener;
	}

	public void setInfoData(List<UserInfo> taskList,
	                        ArrayList<String> webNameArr) {
		this.taskList = taskList;
		this.webNameArr = webNameArr;
	}

	public void updateTaskAdapter(List<UserInfo> taskList) {
		this.taskList = taskList;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return taskList == null ? 0 : taskList.size();
	}

	@Override
	public UserInfo getItem(int position) {
		return taskList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflator = (LayoutInflater) parent.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = (LinearLayout) inflator.inflate(R.layout.list_user,
					null);
			viewHolder.iv_type = (ImageView) convertView
					.findViewById(R.id.imageView1);
			viewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.textView1);
			viewHolder.textView3 = (TextView) convertView
					.findViewById(R.id.textView3);
			viewHolder.firstCharHintTextView = (TextView) convertView
					.findViewById(R.id.text_first_char_hint);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final UserInfo task = taskList.get(position);
		if (!task.getImageData().equals("")) {
			viewHolder.iv_type.setBackgroundDrawable(new BitmapDrawable(Utils
					.toRoundCorner(stringtoBitmap(task.getImageData()), 300)));
		} else {
			viewHolder.iv_type.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.touxiang));
		}
		if (task.getTruename().equals("$"))
			viewHolder.tv_name.setText("");
		else
			viewHolder.tv_name.setText(task.getTruename());
		int idx = position - 1;
		// 判断前后Item是否匹配，如果不匹配则设置并显示，匹配则取消
		char previewChar = idx >= 0 ? webNameArr.get(idx).charAt(0) : ' ';
		char currentChar = webNameArr.get(position).charAt(0);
		// 将小写字符转换为大写字符
		char newPreviewChar = Character.toUpperCase(previewChar);
		char newCurrentChar = Character.toUpperCase(currentChar);
		if (newCurrentChar != newPreviewChar) {
			viewHolder.firstCharHintTextView.setVisibility(View.VISIBLE);
			viewHolder.firstCharHintTextView.setText(String
					.valueOf(newCurrentChar));
		} else {
			// 此段代码不可缺：实例化一个CurrentView后，会被多次赋值并且只有最后一次赋值的position是正确
			viewHolder.firstCharHintTextView.setVisibility(View.GONE);
		}
		viewHolder.textView3.setTag(position);
		viewHolder.textView3.setOnClickListener(listener);
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, FriendsDetailsActivity.class);
				intent.putExtra("type", 1);
				intent.putExtra("phone", task.getUsername());
				context.startActivity(intent);
			}

		});
		return convertView;
	}

	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	public void showT() {
		friend_list.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Toast.makeText(friend_list, "手机号码输入有误，请重新输入。",
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void removeUser() {
		taskList.remove(user_click);
		updateTaskAdapter(taskList);
	}
}